# -*- coding: utf-8 -*-
"""
Created on Fri Jan 25 15:20:21 2019

@author: Administrator
"""

import torch
import torch.nn.functional as F


# replace following class code with an easy sequential network
#这是上一节手动建立的神经网络
class Net(torch.nn.Module):
    def __init__(self, n_feature, n_hidden, n_output):
        super(Net, self).__init__()
        self.hidden = torch.nn.Linear(n_feature, n_hidden)   # hidden layer
        self.predict = torch.nn.Linear(n_hidden, n_output)   # output layer

    def forward(self, x):
        x = F.relu(self.hidden(x))      # activation function for hidden layer
#        x = torch.relu(self.hidden(x))
        x = self.predict(x)             # linear output
        return x

net1 = Net(1, 10, 1)

# easy and fast way to build your network
'''这是利用PyTorch里的方法快速建立的神经网络，与Net1效果是一样'''
net2 = torch.nn.Sequential(
    torch.nn.Linear(1, 10), #相当隐藏层
    #这里的ReLU函数与torch.relu不一样，这是建立神经网络专用的
    torch.nn.ReLU(), #激活函数处理隐藏层神经元
    torch.nn.Linear(10, 1) #处理完后输出层
)


print(net1)     # net1 architecture
"""
Net (
  (hidden): Linear (1 -> 10)
  (predict): Linear (10 -> 1)
)
"""

print(net2)     # net2 architecture
"""
Sequential (
  (0): Linear (1 -> 10)
  (1): ReLU ()
  (2): Linear (10 -> 1)
)
"""

#————————————————————net2的使用————————————————————————
"""
#他不需要像net1 = Net(1, 10, 1)赋值，因为torch.nn.Sequential里面的Linear已经规定好了1,10,1
optimizer = torch.optim.SGD(net2.parameters(),lr=0.2) #把net2的参数直接传入
loss_fun = torch.nn.MSELoss()

plt.ion()
plt.show()

for t in range(100):
    prediction = net2(x) #直接输入x
    loss = loss_fun(prediction,y)
    
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()
    
    if t%5 ==0:
        plt.cla()
        plt.scatter(x.data.numpy(),y.data.numpy())
        plt.plot(x.data.numpy(),y.data.numpy(),'r-',lw=5)
        plt.text(0.5,0,'Loss=%.4f'%loss.data.numpy(),fontdict={'size':20,'color':'red'})
        plt.pause(0.1)
"""

